// 游戏基础逻辑文件
function getPosTop(i, j) {
  return (15 + i * 110) / 50
}
function getPosLeft(i, j) {
  return (15 + j * 110) / 50
}
// 数值的背景色
function getNumberBackgroundColor(number) {
  switch (number) {
    case 2: {
      return '#eee4da'
      break
    }
    case 4: {
      return '#ede0c8'
      break
    }
    case 8: {
      return '#ffa500'
      break
    }
    case 16:
      return '#f59563'
      break
    case 32:
      return '#f67c5f'
      break
    case 64:
      return '#f65e3b'
      break
    case 128:
      return '#edcf72'
      break
    case 256:
      return '#edcc61'
      break
    case 512:
      return '#9c0'
      break
    case 1024:
      return '#33b5e5'
      break
    case 2048:
      return '#09c'
      break
    case 4096:
      return '#a6c'
      break
    case 8192:
      return '#93c'
      break
    case 16384:
      return '#800'
      break
    case 32768:
      return 'red'
      break
    case 65536:
      return '#000'
      break
  }
}
// 数值的字体颜色
function getNumberColor(number) {
  if (number <= 4) return '#8f7a66'
  return '#fff'
}
// 是否能移动
function canMove(board, direction) {
  switch (direction) {
    case 'left': {
      for (let i = 0; i < 4; i++)
        for (let j = 1; j < 4; j++)
          if (board[i][j] !== 0)
            if (board[i][j - 1] === 0 || board[i][j - 1] === board[i][j]) return true
      return false
    }
    case 'right': {
      for (var i = 0; i < 4; i++)
        for (var j = 0; j < 4; j++)
          if (board[i][j] !== 0 && j !== 3)
            if (board[i][j + 1] === 0 || board[i][j + 1] === board[i][j]) return true
      return false
    }
    case 'top': {
      for (var i = 0; i < 4; i++)
        for (var j = 0; j < 4; j++)
          if (board[i][j] !== 0 && i !== 0)
            if (board[i - 1][j] === 0 || board[i - 1][j] === board[i][j]) return true
      return false
    }
    case 'bottom': {
      for (var i = 0; i < 4; i++)
        for (var j = 0; j < 4; j++)
          if (board[i][j] !== 0 && i !== 3)
            if (board[i + 1][j] === 0 || board[i + 1][j] === board[i][j]) return true
      return false
    }
  }
}
// 将判断能否合并的数组值置为0
function isaddedArray() {
  for (var i = 0; i < 4; i++) {
    for (var j = 0; j < 4; j++) {
      added[i][j] = 0
    }
  }
}
// 判断是否有障碍物
function thereAnyObstacles(row, col1, col2, board, direction) {
  if (direction === 'left') {
    for (let i = col1 + 1; i < col2; i++) if (board[row][i] !== 0) return false
  } else if (direction === 'right') {
    for (let i = col1 + 1; i < col2; i++) if (board[row][i] !== 0) return false
  } else {
    for (var i = col1 + 1; i < col2; i++) if (board[i][row] !== 0) return false
  }
  return true
}
function getScore() {
  document.getElementById('score').innerHTML = score
}
// 判断是否还有空间可以移动
function nospace(board) {
  for (var i = 0; i < 4; i++) for (var j = 0; j < 4; j++) if (board[i][j] === 0) return false
  return true
}
function nomove(board) {
  if (
    canMove(board, 'left') ||
    canMove(board, 'right') ||
    canMove(board, 'top') ||
    canMove(board, 'bottom')
  )
    return false
  return true
}
